package com.zzh.partnersys.im.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.google.gson.Gson;
import com.zzh.partnersys.im.message.request.TeamChatRequestMessage;
import com.zzh.partnersys.im.rocketmq.contant.MQConstant;
import com.zzh.partnersys.im.rocketmq.producer.MessageSender;
import com.zzh.partnersys.im.sensitiveword.algorithm.SensitiveWordBs;
import com.zzh.partnersys.im.session.WSTeamSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.List;

/**
 * @author ??????
 * @version 1.0
 */
//ר��ֻ����TeamChatRequestMessage���͵���Ϣ��Handler,����Ⱥ��
@Sharable
public class TeamChatRequestMessageHandler extends SimpleChannelInboundHandler<TeamChatRequestMessage> {
    private Gson gson = new Gson();

    private SensitiveWordBs sensitiveWordBs;

//    private TeamMessageService teamMessageService;

    private MessageSender messageSender;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, TeamChatRequestMessage teamChatRequestMessage) throws Exception {
        System.out.println("teamChatRequestMessage"+ teamChatRequestMessage);
        //��ȡ���ݣ�������Ϣ��??
        if (teamChatRequestMessage == null){

        }
        Long teamId = teamChatRequestMessage.getTeamId();//�����ĸ�??
        String content = teamChatRequestMessage.getContent();
        //�������ʻ��??
        if (sensitiveWordBs == null){
            sensitiveWordBs = getSensitiveWordBs();
        }
        //��������??
        teamChatRequestMessage.setContent(sensitiveWordBs.filter(content));


        //�ж����ߵ���??
        List<Channel> membersChannel = WSTeamSessionFactory.getSession().getMembersChannel(teamId);
        if (!membersChannel.isEmpty()){
            //������??
            for (Channel channel : membersChannel) {
                if (channel != channelHandlerContext.channel()){
                    System.out.println("����");
                    String json = gson.toJson(teamChatRequestMessage);
                    channel.writeAndFlush(new TextWebSocketFrame(json));
                }

            }
        }
        //rocketMQ������??
        if (messageSender == null){
            messageSender = getMessageSender();
        }
        messageSender.sendMsg(MQConstant.CHAT_GROUP_TOPIC,teamChatRequestMessage);



//        if (teamMessageService == null){
//            teamMessageService = getTeamMessageService();
//        }
//        // �����Ϣ��team_message??
//        TeamMessageDO teamMessage = new TeamMessageDO();
//        teamMessage.setTeamId(teamId);
//        teamMessage.setFromUid(fromUid);
//        teamMessage.setContent(content);
//
//
//        //todo ��������(�첽����?)
//        Long id = teamMessageService.insertData(teamMessage);
//        if (id == null){
//            System.out.println("ʧ��");
//            //todo ����ʧ�� ���������������ʧ���أ���
//        }
    }

    //��ȡBean����
    protected SensitiveWordBs getSensitiveWordBs(){
        return SpringUtil.getBean(SensitiveWordBs.class);
    }


    protected MessageSender getMessageSender(){
        return SpringUtil.getBean(MessageSender.class);
    }


//    //��ȡBean����
//    protected TeamMessageService getTeamMessageService(){
//        return SpringUtil.getBean(TeamMessageService.class);
//    }
}
